<template>
  <div class="educationadd">
    <div class="home">
      <div class="navheaders">
        <van-nav-bar @click-left="back">
          <template #left>
            <van-icon name="arrow-left" color="#fff" size="0.6rem" />
          </template>
          <template #title>
            <span>
              <img src="../../assets/img/logo.png" />
            </span>
          </template>
          <!-- <template #right>
            <van-icon name="wap-nav" color="#fff" size="0.6rem" />
          </template>-->
        </van-nav-bar>
      </div>
    </div>

    <div class="myumeremove" v-show="schoolshow&&Operatingshow&&positionshow">
      <div class="educationaddchild">
        <div class="educationaddchildcont">
          <p class="educationaddlift">工作经历</p>
        </div>
        <van-divider color="#E8E8E8" />
        <van-form @submit="onSubmit" label-align="left">
          <van-field readonly v-model="formdata.company" name="gsmc" @click="corporateName('company','公司名称')"
            label="公司名称" placeholder="请输入公司名称" :rules="[{ required: true, message: ' ' },
              { pattern: /^[^\s]*$/, message: ' '}]" />

          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly v-model="formdata.job" name="zwmc" @click="corporateName('job','职位名称')" label="职位名称"
            placeholder="请输入职位名称" :rules="[{ required: true, message: ' ' },  { pattern: /^[^\s]*$/, message: ' '}]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly v-model="formdata.jobCategoryname" name="jobCategory"
            @click="corporateposs('jobCategory','职位类别')" label="职位类别" placeholder="请输入职位类别"
            :rules="[{ required: true, message: ' ' }]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly clickable :value="schoolstarttime" label="参加工作开始时间" placeholder="请选择开始时间"
            @click="showTime = true" :rules="[{ required: true, message: ' ' }]" />
          <van-popup v-model="showTime" position="bottom">
            <van-datetime-picker type="date" v-model="currentDate" :item-height="45" :maxDate="maxDatestaTm"
              :minDate="minDate" @confirm="onConfirmtimestart" @cancel="showTime = false" />
          </van-popup>
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly clickable :value="schoolendtime" label="参加工作结束时间" placeholder="请选择开始时间"
            @click="showendTime = true" :rules="[{ required: true, message: ' ' }]" />
          <van-popup v-model="showendTime" position="bottom">
            <van-datetime-picker v-model="currentDate" type="date" :item-height="45" :maxDate="maxDate"
              :minDate="minDateendTm" @confirm="onConfirmtimeend" @cancel="showendTime = false" />
          </van-popup>

          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field v-model="formdata.preTaxWage" name="preTaxWage" label="税前月薪" type="digit" :maxlength="7"
            placeholder="请输入税前月薪" :rules="[{ required: true, message: ' ' },
            {validator,message: '税前月薪不能超过5000000'}]" />
          <!-- <van-divider color="#E8E8E8" /> -->
          <van-field readonly v-model="formdata.remark" name="gzzz" @click="positionCategory('remark')" label="工作职责"
            placeholder="请输入工作职责" :rules="[{ required: true, message: ' ' }]" />
          <div style="margin: 16px;">
            <van-button round block type="info" :style="{width:formdata.id!==''?'3rem':'8rem'}"
              :disabled="buttondisabled" native-type="submit">保存
            </van-button>
          </div>
        </van-form>
        <van-button round block type="info" style="width: 3rem;float: left;margin-left: 0.3rem;" v-if="formdata.id!==''"
          @click="removeeducat">删除该经历</van-button>
        <van-divider color="#E8E8E8" />
      </div>
    </div>

    <div>
      <experience-add ref="ExperienceAdd" v-on:industryca="industryca" v-show="!schoolshow"></experience-add>
    </div>
    <div>
      <operating-duty ref="OperatingDuty" v-on:industryca="industryca" v-show="!Operatingshow"></operating-duty>
    </div>
    <position-desired ref="PositionDesired" type="digit" v-on:positionon="positionon" v-show="!positionshow">
    </position-desired>
  </div>
</template>
<script>
import ExperienceAdd from '@/components/experienceAdd/experienceAdd'
import RESUMEMANAGEMENT from '@/api/ResumeManagement'
import OperatingDuty from '@/components/experienceAdd/operatingDuty'
import PositionDesired from '@/components/resumeManagement/positionDesired'
export default {
  components: {
    ExperienceAdd,
    OperatingDuty,
    PositionDesired
  },
  data () {
    return {
      positionshow: true,
      schoolshow: true,
      Operatingshow: true,
      showTime: false,
      showendTime: false,
      buttondisabled: false,
      maxDate: new Date(),
      minDate: new Date(1970, 0, 1),
      maxDatestaTm: new Date(),
      minDateendTm: new Date(1970, 0, 1),
      bridtime: '',
      currentDate: new Date(),
      message: '',
      schoolstarttime: '',
      schoolendtime: '',
      resumeid: '',
      formdata: {
        jobCategory: [], // 职位类别
        company: '', // 公司名称
        job: '', // 职位名称
        preTaxWage: '', // 税前月薪
        remark: '', // 工作职责
        jobCategoryname: '',
        id: ''
      }
    }
  },
  computed: {},
  methods: {
    // 传入子组件值
    corporateName (namecode, name) {
      this.schoolshow = false
      let counte
      if (namecode === 'company') {
        counte = this.formdata.company
      } else if (namecode === 'jobCategory') {
        counte = this.formdata.jobCategory[0]
      } else if (namecode === 'job') {
        counte = this.formdata.job
      } else if (namecode === 'remark') {
        counte = this.formdata.remark
      }
      this.$refs.ExperienceAdd.substate(namecode, name, counte)
    },
    positionCategory (namecode) {
      this.Operatingshow = false
      this.$refs.OperatingDuty.substate(namecode, this.formdata.remark)
    },
    // 传入子组件的参数
    corporateposs () {
      this.positionshow = !this.positionshow
      console.log(this.formdata.jobCategory)
      if (this.formdata.jobCategory.length !== 0) {
        this.$refs.PositionDesired.substate(
          this.formdata.jobCategoryname,
          this.formdata.jobCategory[2].split()
        )
      } else {
        this.$refs.PositionDesired.substate(
          this.formdata.jobCategoryname,
          this.formdata.jobCategory
        )
      }
    },
    // 获取子组件传过来的值
    positionon (num, arr, list) {
      console.log(list, 'list')
      console.log(arr, 'arr')
      this.positionshow = !this.positionshow
      this.formdata.jobCategoryname = arr[0].name
      // this.formdata.jobCategory[0] = arr[0].code;
      // 循环获取一级裁断
      list.map(item => {
        // 循环获取二级裁断
        item.childVo.map((val, i) => {
          // 循环获取三级菜单
          item.childVo[i].childVo.map((value, j) => {
            // 判断最code是否相等，如果相等获取父级code和爷爷级code
            if (arr[0].code === item.childVo[i].childVo[j].code) {
              this.formdata.jobCategory = []
              this.formdata.jobCategory.push(
                item.code,
                item.childVo[i].code,
                item.childVo[i].childVo[j].code
              )
            }
          })
        })
      })
      console.log(this.formdata.jobCategory, 'jobCategory')
      this.$forceUpdate()
    },
    // 验证税前薪资
    validator (val) {
      return val <= 5000000
    },
    resumeidjy (resumeid) {
      this.resumeid = resumeid
    },
    // 接收子组件传过来的参数
    industryca (namecode, name, id) {
      console.log(namecode, name)
      this.schoolshow = true
      this.Operatingshow = true
      if (namecode === 'company') {
        this.formdata.company = name
      } else if (namecode === 'jobCategory') {
        this.formdata.jobCategory[0] = id
        this.formdata.jobCategoryname = name
      } else if (namecode === 'job') {
        this.formdata.job = name
      } else if (namecode === 'remark') {
        this.formdata.remark = name
      }
    },
    Resume () { },
    back () {
      if (!this.schoolshow || !this.Operatingshow) {
        this.schoolshow = true
        this.Operatingshow = true
      } else {
        this.$router.go(-1)
      }
    },
    onConfirmtimestart (date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.schoolstarttime = `${year}-${month}-${day}`
      let timedata = new Date(this.schoolstarttime).getTime()
      timedata = new Date(timedata)
      this.minDateendTm = new Date(
        timedata.getFullYear(),
        timedata.getMonth(),
        timedata.getDate()
      )
      console.log(this.maxDatestaTm)
      this.$forceUpdate()
      //  this.inputForm.cjgzdatetim = `${date.getMonth() + 1}-${date.getDate()}`
      this.showTime = false
    },
    onConfirmtimeend (date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.schoolendtime = `${year}-${month}-${day}`
      let timedata = new Date(this.schoolendtime).getTime()
      timedata = new Date(timedata)
      this.maxDatestaTm = new Date(
        timedata.getFullYear(),
        timedata.getMonth(),
        timedata.getDate()
      )
      //  this.inputForm.cjgzdatetim = `${date.getMonth() + 1}-${date.getDate()}`
      this.showendTime = false
    },
    // 工作经历新增修改
    onSubmit () {
      let params = this.formdata
      params.startTime = this.schoolstarttime + ' 00:00:00'
      if (this.schoolendtime !== '') {
        params.endTime = this.schoolendtime + ' 00:00:00'
      }
      params.jobCategory = JSON.stringify(this.formdata.jobCategory)
      params.resumeId = this.resumeid
      RESUMEMANAGEMENT.insertWork(params).then(res => {
        this.buttondisabled = false
        console.log(res.data)
        if (res.data.code === 200) {
          this.$router.push({
            name: 'resumeRemove',
            query: {
              resumeid: this.resumeid
            }
          })
        } else {
          this.$notify(res.data.msg)
        }
      })
    },
    // 工作经历删除
    removeeducat () {
      let that = this
      this.$dialog
        .confirm({
          title: '工作经历删除',
          message: '确定要删除该段经历么'
        })
        .then(() => {
          console.log(1)
          RESUMEMANAGEMENT.deleteWork(that.formdata.id).then(res => {
            console.log(res.data)
            if (res.data.code === 200) {
              that.$router.push({
                name: 'resumeRemove',
                query: {
                  resumeid: that.resumeid
                }
              })
            }
          })
        })
        .catch(() => {
          // on cancel
        })
    },
    worktime () {
      this.$router.push({
        name: 'worktime'
      })
    }
  },
  created () {
    if (this.$route.query.resumeid) {
      this.resumeid = this.$route.query.resumeid
    }
    if (this.$route.query.id) {
      let listobjet = JSON.parse(sessionStorage.getItem('workExperienceAdd'))
      console.log(listobjet)
      this.formdata.id = listobjet.id
      this.formdata.jobCategory = JSON.parse(listobjet.jobCategory)
      this.formdata.jobCategoryname = listobjet.jobCategoryText
      this.formdata.job = listobjet.job
      this.formdata.company = listobjet.company
      this.formdata.preTaxWage = listobjet.preTaxWage.split('元')[0]
      this.formdata.remark = listobjet.remark
      this.schoolstarttime = /\d{4}-\d{1,2}-\d{1,2}/g
        .exec(listobjet.startTime)
        .toString()

      this.schoolendtime = /\d{4}-\d{1,2}-\d{1,2}/g
        .exec(listobjet.endTime)
        .toString()
      this.$forceUpdate()
    }
  }
}
</script>
<style scoped></style>
